package com.dmwork.service.interaction.controller.front.me;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.interaction.entity.WorkComment;
import com.dmwork.service.interaction.entity.WorkCommentLike;
import com.dmwork.service.interaction.entity.vo.WorkCommentLikeMessageVo;
import com.dmwork.service.interaction.feign.CustomerFeignService;
import com.dmwork.service.interaction.feign.MessageFeignService;
import com.dmwork.service.interaction.mq.sender.MessageSender;
import com.dmwork.service.interaction.service.WorkCommentLikeService;
import com.dmwork.service.interaction.service.WorkCommentService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 作品评论的点赞表 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-04-26
 */

@Slf4j
@Api(description = "个人点赞功能管理")
@Transactional
@RestController
@RequestMapping("/front/dm_work/interaction/work_comment_like/me")
public class FrontMeWorkCommentLikeController {

    @Autowired
    private WorkCommentLikeService workCommentLikeService;
    @Autowired
    private WorkCommentService workCommentService;
    @Autowired
    private MessageSender messageSender;

    @ApiOperation("为指定评论点赞或取消点赞")
    @PutMapping("like/{commentId}/{like}")
    public R like(HttpServletRequest request, @PathVariable String commentId, @PathVariable Integer like) {
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        //获取评论详情
        WorkComment workComment = workCommentService.getById(commentId);

        if (like == -1) { //如果like==-1，说明是取消点赞，要将work_comment_like表中的点赞记录删除，并将对应评论的like_count-1
            QueryWrapper<WorkCommentLike> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("comment_id", commentId);
            queryWrapper.eq("customer_id", jwtInfo.getId());
            WorkCommentLike workCommentLike = workCommentLikeService.getOne(queryWrapper);
            workCommentLikeService.removeById(workCommentLike.getId());
            //更新对应评论的点赞数
            workCommentService.decreaseLikeCount(commentId);
            String customerId = workComment.getCustomerId();
            if (!customerId.equals(jwtInfo.getId())) {
                messageSender.sendCommentLikeMessage(customerId, workCommentLike.getId(), 0, 0);
            }
            return R.ok().message("取消点赞成功");
        } else {
            //保存新的点赞信息
            WorkCommentLike workCommentLike = new WorkCommentLike();
            workCommentLike.setCustomerId(jwtInfo.getId());
            workCommentLike.setCommentId(commentId);
            workCommentLikeService.save(workCommentLike);
            //更新对应评论的点赞数
            workCommentService.increaseLikeCount(commentId);
            // 为被点赞用户创建一条消息
            String customerId = workComment.getCustomerId();
            if (!customerId.equals(jwtInfo.getId())) {
                messageSender.sendCommentLikeMessage(workComment.getCustomerId(), workCommentLike.getId(), 0, 1);
            }
            return R.ok().message("点赞成功");
        }
    }

}

